package com.xxj.puzzlesheep;

import android.view.KeyEvent;
import android.view.MotionEvent;

import com.xxj.puzzlesheep.core.LGraphics;
import com.xxj.puzzlesheep.core.LImage;
import com.xxj.puzzlesheep.core.LScreen;


public class PuzzleGameScreen extends LScreen {

	private LImage imageBack, tmp_imageBack, imageForward;

	private LGraphics tmp_graphics;

	private int blocks[];

	private boolean isEvent;

	private int count, rs, cs, row, col, width, height;
	
	private PuzzleMain main;

	public PuzzleGameScreen(String file1, String file2, int row, int col,PuzzleMain main) {
		this.col = col;
		this.row = row;
		this.imageBack = getLAImage(file1);
		this.width = imageBack.getWidth();
		this.height = imageBack.getHeight();
		this.rs = width / row;
		this.cs = height / col;
		this.tmp_imageBack = new LImage(width, height + cs);
		this.tmp_graphics = tmp_imageBack.getLAGraphics();
		this.count = col * row;
		this.blocks = new int[count];
		this.imageForward = getLAImage(file2);
		for (int i = 0; i < count; i++) {
			blocks[i] = i;
		}

		this.main = main;
		long start = System.currentTimeMillis();
		rndBlocks();
		System.out.println("rndBlocks() cost time(s):" + (System.currentTimeMillis() - start) / 1000);
	}

	/**
	 * 复制拼图中图片块
	 * 
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 */
	private void copy(int x1, int y1, int x2, int y2) {
		tmp_graphics.copyArea(x1 * rs, y1 * cs, rs, cs, (x2 - x1) * rs,
				(y2 - y1) * cs);
	}

	/**
	 * 随机生成拼图内容
	 * 
	 */
	private void rndBlocks() {
		tmp_graphics.drawImage(imageBack, 0, 0);
		for (int i = 0; i < (count * row); i++) {
			int x1 = (int) ((double) row * Math.random());
			int y1 = (int) ((double) col * Math.random());
			int x2 = (int) ((double) row * Math.random());
			int y2 = (int) ((double) col * Math.random());
			copy(x1, y1, 0, col);
			copy(x2, y2, x1, y1);
			copy(0, col, x2, y2);
			int j1 = blocks[y1 * row + x1];
			blocks[y1 * row + x1] = blocks[y2 * row + x2];
			blocks[y2 * row + x2] = j1;
		}
	}

	/**
	 * 点击触摸屏
	 */
	public boolean onTouchDown(MotionEvent e) {
		if (isEvent) {
			return isEvent;
		}
		int x = (int) (e.getX() / rs);
		int y = (int) (e.getY() / cs);
		copy(0, 0, 0, col);
		copy(x, y, 0, 0);
		copy(0, col, x, y);
		int no = blocks[0];
		blocks[0] = blocks[y * row + x];
		blocks[y * row + x] = no;
		int index;
		for (index = 0; index < count; index++) {
			if (blocks[index] != index) {
				break;
			}
		}
		if (index == count) {
			isEvent = true;
		}
		return isEvent;
	}

	/**
	 * 绘制拼图
	 */
	public void draw(LGraphics g) {
		if (!isEvent) {
			g.drawImage(tmp_imageBack, 0, 0);
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++)
					g.drawRect(i * rs, j * cs, rs, cs);

			}
		}
		if (isEvent && imageForward != null) {
			g.drawImage(imageBack, 0, 0);
			g.drawImage(imageForward, 0, 0);
			tmp_graphics.dispose();
		}

	}

	public boolean isEvent() {
		return isEvent;
	}

	public void setEvent(boolean isEvent) {
		this.isEvent = isEvent;
	}

	public boolean onKeyDown(int keyCode, KeyEvent e) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			main.getView().setScreen(new PuzzleMenuScreen("menu.jpg", main));
			break;

		default:
			break;
		}
		return true;
	}

	public boolean onKeyUp(int keyCode, KeyEvent e) {
		return false;
	}

	public boolean onTouchMove(MotionEvent e) {
		return false;
	}

	public boolean onTouchUp(MotionEvent e) {
		return false;
	}

}
